package com.xinqi.modules.homework.homework.thread;

import com.xinqi.common.core.constant.Constants;
import com.xinqi.common.core.enums.RocketMqSliceEnum;
import com.xinqi.common.third.oss.slice.event.OssSliceEvent;
import com.xinqi.modules.homework.homework.domain.CourseHomeworkFileEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 提交作业切片生成
 *
 * @author dzy
 */
@Service
@RequiredArgsConstructor
public class SliceHomeworkFileService {

    private final OssSliceEvent ossSliceEvent;

    public void updateHomeworkFileSlice(List<CourseHomeworkFileEntity> list) {
        list.forEach(file -> {
            if (checkFile(file.getFileurl())) {
                ossSliceEvent.createSliceImage(file.getFileurl(), file.getId(), RocketMqSliceEnum.HOMEWORK_SLICE.getCode());
            }
        });
    }


    private boolean checkFile(String url) {
        // 获取文件后缀名
        String[] strArray = url.split("\\.");
        int suffixIndex = strArray.length - 1;
        String suffix = strArray[suffixIndex].toLowerCase();
        if (suffix.contains(Constants.DOC_SUF) || suffix.equals(Constants.PDF_SUF)
                || suffix.contains(Constants.PPT_SUF) || suffix.contains(Constants.XLS_SUF)
                || suffix.equals(Constants.TXT_SUF)) {
            return true;
        } else {
            return false;
        }
    }

}
